package com.sneakxy.mybase4j.platform.config;

import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;

import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;

@Configuration
@PropertySource({
	"classpath:dubbo.properties"
})
@DubboComponentScan(basePackages = "com.sneakxy.mybase4j.platform.provider")
public class DubboConfiguration implements EnvironmentAware {

	private Environment env;
	
	@Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(env.getProperty("dubbo.application"));
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress(env.getProperty("dubbo.address"));
        registryConfig.setClient("curator");
        return registryConfig;
    }
    
    @Bean
    public ProtocolConfig protocolConfig() {
    	// 服务提供者协议配置
    	ProtocolConfig protocol = new ProtocolConfig();
    	protocol.setName("dubbo");
    	protocol.setPort(env.getProperty("dubbo.port", Integer.class));
    	protocol.setSerialization(env.getProperty("dubbo.serialization", "kryo"));
    	protocol.setThreads(200);
    	return protocol;
    }
    
    @Override
	public void setEnvironment(Environment environment) {
		env = environment;
	}
    
}
